<?php
  global $cfg;

  $odir = getcwd();
  $tmp = dirname(__FILE__);
  chdir($tmp);
  chdir("..");
  $pth = getcwd();
  chdir($odir);


  //$pth = dirname(__FILE__);


define("CORE_PLUGINS_FOLDER", "/include/plugins/");
define("CORE_BBPLUGINS_FOLDER", "/include/facePlugins/");
define("CORE_SNIPPETS_FOLDER", "/include/snippets/");
define("CORE_CONFIG", "/include/config.php");

$cfg = parse_ini_file($pth . CORE_CONFIG, 1);

  //$cfg = parse_ini_file($pth.'/lib/config.php', 1);
  $cfg["file_root"] = $pth;
  $cfg["system"]["file_root"] = $pth;
  $cfg["http_root"] = $cfg["system"]["http_root"];



  // ----------------------------------------------------------
  //   Database operations
  // ----------------------------------------------------------
  global $db;
  $db = mysql_connect($cfg['DB']['hostspec'], $cfg['DB']['username'], $cfg['DB']['password']) or die("Can't connect to the database");

  mysql_select_db($cfg['DB']['database'], $db);


  /**
   * Get All rows from the SQL request and return array of the row's
   *
   * @param string $sql
   * @return array of the rows
   */
  function getAll($sql, $mode=0) {
    $r = @mysql_query($sql) or die("Error in SQL ({$sql}): ".mysql_error());
    $rw = array();
    while($r!=FALSE && $row = mysql_fetch_array($r, $mode ? MYSQL_ASSOC : MYSQL_BOTH)) {
      $rw[] = $row;
    }
    return $rw;
  }

  /**
   * Get one row from the SQL request and return is as assoc array
   *
   * @param string $sql
   * @return row as assoc array
   */
  function getRow($sql) {
    global $db;

    $r = @mysql_query($sql) or die("Error in SQL ({$sql}): ".mysql_error());
    $rw = array();
    while($r!=FALSE && $row = mysql_fetch_array($r)) {
      return $row;
    }
    return array();
  }
                                                                                                      
  /**
   * Return one value from the SQL request
   *
   * @param string $sql
   * @return value
   */
  function getOne($sql) {
    global $db;

    $r = @mysql_query($sql) or die("Error in SQL ({$sql}): ".mysql_error());
    while($r!=FALSE && $row = mysql_fetch_array($r)) {
//    	print_r($row);
      return $row[0];
    }
    return null;
  }

  /**
   * Execute the SQL request and return MySQL result
   *
   * @param string $sql
   * @return mysql result
   */
  function dbExec($sql) {
	$r = mysql_query($sql) or die("Error in SQL:\n{$sql}\n\n".mysql_error());
	return $r;
  }



  // ----------------------------------------------------------
  //   Safe String Operation
  // ----------------------------------------------------------

  
  // ----------------------------------------------------------
  //   Safe array operation
  // ----------------------------------------------------------


  // ----------------------------------------------------------
  //   Debug feature
  /**
  * @return isset($var) and !empty($var) BOOL
  * @param variable for test
  * @desc returns isset and !empty
  */


// ------------------------------------------------------------
// -- Short-text aliases
// ------------------------------------------------------------

?>